import { applyDecorators, MethodNotAllowedException, SetMetadata, UseGuards, UseInterceptors } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';

// type接收一个字符串的数据，用于判断文件类型是否支持上传
export function fileFilter(type:string[]){
    return (req, file, cb) =>{
        console.log(req,);

      const check =   type.some(t=>file.mimetype.includes(t))
        
        console.log(file.mimetype)
        if(!(check)){
       return cb(new MethodNotAllowedException('文件类型不允许'),false)
    
        }
        // return file
        return cb(null,true)
      }
}

// 用于给文件添加选项，比如文件大小控制等，在option中配置
export function Upload(field="file",option:MulterOptions) {
  return applyDecorators(
    UseInterceptors(FileInterceptor(field,option)));
}

// 封装的通用上传装饰器,想要什么格式进行长传就在type中传递字符串的数组
export function UploadedFiles(file="flie",type:string[]){
    return Upload(file,{
        limits:{
            fieldSize:Math.pow(1024 , 2) * 3
        },
        fileFilter:fileFilter(type)
    })
}